/**
 * copyright (c) 2017-2023 厦门风领科技有限公司
 * https://www.fengling.cn/
 * 
 * 字节小游戏适配
 * zengbinsi
 * 2020-08-15 15:33:07
 * 2023-10-23 15:32:49
 */
window.gg = window.gg || {};

if (gg.isMiniGameTT) {
    console.log('[TS-API] ===>>>', '适配字节小游戏平台');

    window['__wxConfig'] = window['__wxConfig'] || {};
    if (tt.env && tt.env.VERSION) {
        switch (tt.env.VERSION) {
            case 'development': window['__wxConfig'].envVersion = 'develop'; break;
            case 'preview': window['__wxConfig'].envVersion = 'trial'; break;
            case 'production': window['__wxConfig'].envVersion = 'release'; break;

            default: window['__wxConfig'].envVersion = 'release'; break;
        }
    }

    gg.channel = cc.sys.os === cc.sys.OS_IOS ? gg.EChannelType.MINI_GAME_TT_IOS : gg.EChannelType.MINI_GAME_TT_AND;

    gg.onShow((option) => {
        gg.log('游戏唤醒参数:', option);

        gg.checkUpdateVersion();
        gg.launchOption = option || {};
        gg.systemEvent.emit('WECHAT_MINI_GAME_SHOW', gg.launchOption);
    });

    gg.onHide((res) => {
        gg.log('游戏暂停参数:', res);
    });

    // https://microapp.bytedance.com/docs/zh-CN/mini-game/develop/api/payment/tt-request-game-payment
    gg.payment = (data) => {
        return new Promise(async (resolve, reject) => {
            if (cc.sys.os === cc.sys.OS_IOS) {
                gg.showToast({
                    title: 'iOS暂不支持支付',
                    icon: 'none'
                });
                const err = { errMsg: 'iOS暂不支持支付', errCode: -1 };
                data.fail && data.fail(err);
                return reject(err);
            }

            const object = {
                mode: 'game',
                env: 0,
                offerId: data.offerId || '',
                currencyType: data.currencyType || 'CNY',
                platform: data.platform || 'android',
                buyQuantity: data.buyQuantity,
                zoneId: `${data.zoneId}` || 1,
                customId: data.customId || data.orderNo || data.orderId,
                extraInfo: data.ext || data.extraInfo,
                success: (res) => {
                    gg.systemEvent.emit('PAY_SHOW_LODING', data);
                    data.success && data.success(res);
                    resolve(res);
                },
                fail: (err) => {
                    data.fail && data.fail(err);
                    reject(err);
                },
                complete: (res) => {
                    data.complete && data.complete(res);
                },
            };

            gg.requestGamePayment(object);
        });
    };

    // https://microapp.bytedance.com/docs/zh-CN/mini-game/develop/api/retweet/tt-share-app-message
    gg.shareAppMessage = (data) => {
        gg.log('share pull...', data);
        return new Promise((resolve, reject) => {
            const options = {
                title: data.title,
                query: data.query,
                imageUrl: data.imageUrl,
                desc: data.desc,
                success: (res) => {
                    gg.log('share successed.');
                    data && data.success && data.success(res);
                    resolve(res);
                },
                fail: (err) => {
                    gg.log('share fail.');
                    data && data.fail && data.fail(err);
                    reject(err);
                }
            };

            // 字节分享视频
            gg.isEmpty(data.templateId) || (options.templateId = data.templateId);
            gg.isEmpty(data.channel) || (options.channel = data.channel);
            gg.isEmpty(data.extra) || (options.extra = data.extra);

            tt.shareAppMessage(options);
        });
    };

    // 激励式广告场景曝光
    gg.onReportUserBehaviorBranchAnalytics = (data) => { gg.log('该平台暂未实现gg.reportUserBehaviorBranchAnalytics()方法。'); };
}

